package com.gqj.meter.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gqj.common.annotation.Log;
import com.gqj.common.core.controller.BaseController;
import com.gqj.common.core.domain.AjaxResult;
import com.gqj.common.enums.BusinessType;
import com.gqj.meter.domain.WarningConfig;
import com.gqj.meter.service.IWarningConfigService;
import com.gqj.common.utils.poi.ExcelUtil;
import com.gqj.common.core.page.TableDataInfo;

/**
 * 报警配置Controller
 *
 * @author lxq
 * @date lxq-02-10
 */
@Api(value = "/meter/warningConfig", description = "报警配置")
@RestController
@RequestMapping("/meter/warningConfig")
public class WarningConfigController extends BaseController
{
    @Autowired
    private IWarningConfigService warningConfigService;

    /**
     * 查询报警配置列表
     */
    @ApiOperation("查询报警配置列表")
    @PreAuthorize("@ss.hasPermi('meter:warningConfig:list')")
    @GetMapping("/list")
    public TableDataInfo list(WarningConfig warningConfig)
    {
        startPage();
        List<WarningConfig> list = warningConfigService.selectWarningConfigList(warningConfig);
        return getDataTable(list);
    }

    /**
     * 导出报警配置列表
     */
    @PreAuthorize("@ss.hasPermi('meter:warningConfig:export')")
    @Log(title = "报警配置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WarningConfig warningConfig)
    {
        List<WarningConfig> list = warningConfigService.selectWarningConfigList(warningConfig);
        ExcelUtil<WarningConfig> util = new ExcelUtil<WarningConfig>(WarningConfig.class);
        util.exportExcel(response, list, "报警配置数据");
    }

    /**
     * 获取报警配置详细信息
     */
    @ApiOperation("获取报警配置详细信息")
    @PreAuthorize("@ss.hasPermi('meter:warningConfig:query')")
    @GetMapping(value = "/{configId}")
    public AjaxResult getInfo(@PathVariable("configId") Long configId)
    {
        return success(warningConfigService.selectWarningConfigByConfigId(configId));
    }

    /**
     * 新增报警配置
     */
    @ApiOperation("新增报警配置")
    @PreAuthorize("@ss.hasPermi('meter:warningConfig:add')")
    @Log(title = "报警配置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WarningConfig warningConfig)
    {
        return toAjax(warningConfigService.insertWarningConfig(warningConfig));
    }

    /**
     * 修改报警配置
     */
    @ApiOperation("修改报警配置")
    @PreAuthorize("@ss.hasPermi('meter:warningConfig:edit')")
    @Log(title = "报警配置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WarningConfig warningConfig)
    {
        if(warningConfig.getLowerLimit() >= warningConfig.getUpperLimit()){
            return AjaxResult.error("修改配置失败，下限大于或等于上限");
        }
        return toAjax(warningConfigService.updateWarningConfig(warningConfig));
    }

    /**
     * 删除报警配置
     */
    @ApiOperation("删除报警配置")
    @PreAuthorize("@ss.hasPermi('meter:warningConfig:remove')")
    @Log(title = "报警配置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable Long[] configIds)
    {
        return toAjax(warningConfigService.deleteWarningConfigByConfigIds(configIds));
    }
}
